package my.security.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import my.security.domain.Employee;
import my.security.domain.MenuItem;
import my.security.domain.Organization;
import my.security.domain.Role;
import my.security.service.ISecurityService;
import my.security.util.RequestUtil;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Validate;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonProperty;

@Controller
@RequestMapping()
@Transactional
public class SecurityController {

	private static final Logger LOGGER = Logger.getLogger(SecurityController.class);

	// 如果使用了自定义的类型，或者enum，进行了数据类型的转换，需要在这里注册，然后方可以使用
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		binder.registerCustomEditor(Organization.class, new OrganizationTreeEditor(true));
		binder.registerCustomEditor(MenuItem.class, new MenuItemTreeEditor(true));
		binder.registerCustomEditor(Role.class, new RoleTreeEditor(true));
		binder.registerCustomEditor(Employee.class, new EmployeeEditor(true));
	}

	@Resource(name = "securityService")
	private ISecurityService securityService;

	public void setSecurityService(ISecurityService securityService) {
		this.securityService = securityService;
	}

	private static final String ERROR_PREIX = "error";
	private static final String OK_PREIX = "ok";
	private static final String FAIL_PREIX = "fail";
	private static final String BOOLEAN_OK = "操作成功";
	private static final String BOOLEAN_FAIL = "操作失败";

	private String currentRequestUrl() {
		HttpServletRequest req = (HttpServletRequest) RequestContextHolder.getRequestAttributes().resolveReference("request");
		return req.getPathInfo(); // 获取当前请求的mapping路径
	}
	
	private TrueResult integerTrueResult(Integer id) {
		if (id == null) {
			return new TrueResult(currentRequestUrl(), FAIL_PREIX, BOOLEAN_FAIL);
		} else {
			return new TrueResult(currentRequestUrl(), OK_PREIX, id);
		}
	}
	
	private TrueResult commonTrueResult(Object result) {
		return new TrueResult(currentRequestUrl(), OK_PREIX, result);
	}
	
	private TrueResult booleanTrueResult(boolean isSucess) {
		String state = FAIL_PREIX;
		Object result = null;
		if (isSucess) {
			state = OK_PREIX;
			result = BOOLEAN_OK;
		} else {
			state = FAIL_PREIX;
			result = BOOLEAN_FAIL;
		}
		return new TrueResult(currentRequestUrl(), state, result);
	}
	
	private TrueResult errorTrueResult(String errorMessage) {
		return new TrueResult(currentRequestUrl(), ERROR_PREIX, errorMessage);
	}
	
	@RequestMapping(value = "/organization/add-tree")
	@ResponseBody
	public TrueResult addOrganizationTree(@RequestParam Organization organization) {
		try {
			return integerTrueResult(securityService.addOrUpdateOrganization(organization));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/organization/add/by-json")
	@ResponseBody
	public TrueResult addOrganizationByJson(@RequestParam Organization organization) {
		return addOrganization(organization);
	}

	@RequestMapping(value = "/organization/add")
	@ResponseBody
	public TrueResult addOrganization(@ModelAttribute Organization organization) {
		try {
			organization.setChildren(null); // 避免保存树状结构 没有必要 映射控制
			return integerTrueResult(securityService.addOrUpdateOrganization(organization));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/organization/remove")
	@ResponseBody
	public TrueResult removeOrganization(@RequestParam int id) {
		try {
			return booleanTrueResult(securityService.removeOrganization(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取指定组织机构 id不能为空 */
	@RequestMapping(value = "/organization/one")
	@ResponseBody
	public TrueResult queryOrganization(final @RequestParam Integer id) {
		try {
			Validate.notNull(id);
			return commonTrueResult(securityService.queryOrganizationById(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/organization/all-roots")
	@ResponseBody
	@Transactional
	public TrueResult queryAllRootOrganizations() {
		try {
			return commonTrueResult(securityService.queryAllRootOrganizations());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/organization/all-roots-tree")
	@ResponseBody
	public TrueResult queryAllRootOrganizationsWithChildren() {
		try {
			return commonTrueResult(securityService.queryAllRootOrganizationsWithChildren());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/organization/children")
	@ResponseBody
	public TrueResult queryChildrenForOrganization(@RequestParam int organizationId) {
		try {
			return commonTrueResult(securityService.queryChildrenForOrganization(organizationId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 增加员工到机构中 */
	@RequestMapping(value = "/organization/add-employees")
	@ResponseBody
	public TrueResult addEmployeesToOrganization(final @RequestParam int organizationId, final @RequestParam String emploeeIds) {
		try {
			return booleanTrueResult(securityService.addEmployeesToOrganization(organizationId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(emploeeIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 删除员工从机构中 */
	@RequestMapping(value = "/organization/remove-employees")
	@ResponseBody
	public TrueResult removeEmployeesFromOrganization(final @RequestParam int organizationId, final @RequestParam String emploeeIds) {
		try {
			return booleanTrueResult(securityService.removeEmployeesFromOrganization(organizationId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(emploeeIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 增加角色到机构中 */
	@RequestMapping(value = "/organization/add-roles")
	@ResponseBody
	public TrueResult addRolesToOrganization(final @RequestParam int organizationId, final @RequestParam String roleIds) {
		try {
			return booleanTrueResult(securityService.addRolesToOrganization(organizationId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(roleIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 删除角色从机构中 */
	@RequestMapping(value = "/organization/remove-roles")
	@ResponseBody
	public TrueResult removeRolesFromOrganization(final @RequestParam int organizationId, final @RequestParam String roleIds) {
		try {
			return booleanTrueResult(securityService.removeRolesFromOrganization(organizationId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(roleIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/employee/add/by-json")
	@ResponseBody
	public TrueResult addEmployeeByJson(@RequestParam Employee employee) {
		return addEmployee(employee);
	}

	@RequestMapping(value = "/employee/add")
	@ResponseBody
	public TrueResult addEmployee(@ModelAttribute Employee employee) {
		try {
			return integerTrueResult(securityService.addOrUpdateEmployee(employee));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/employee/{employeeId}/set-photo")
	@ResponseBody
	public TrueResult updateEmployeePhoto(@PathVariable int employeeId, @RequestParam MultipartFile picture) {
		try {
			Employee employee = securityService.queryEmployeeById(employeeId);
			employee.setPhoto(picture.getBytes());
			return integerTrueResult(securityService.addOrUpdateEmployee(employee));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/employee/remove")
	@ResponseBody
	public TrueResult removeEmployee(@RequestParam int id) {
		try {
			return booleanTrueResult(securityService.removeEmployee(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取员工 id不能为空 */
	@RequestMapping(value = "/employee/one")
	@ResponseBody
	public TrueResult queryEmployee(final @RequestParam Integer id) {
		try {
			Validate.notNull(id);
			return commonTrueResult(securityService.queryEmployeeById(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取全部员工 */
	@RequestMapping(value = "/employee/all")
	@ResponseBody
	public TrueResult queryAllEmployees() {
		try {
			return commonTrueResult(securityService.queryAllEmployees());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 分页--获取全部员工 */
	@RequestMapping(value = "/employee/all/page")
	@ResponseBody
	public TrueResult pageQueryAllEmployees(@RequestParam int pageNum, @RequestParam int pageSize) {
		try {
			return commonTrueResult(securityService.queryAllEmployees(pageNum, pageSize));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 根据员工的name来筛选 模糊匹配 */
	@RequestMapping(value = "/employee/by-name")
	@ResponseBody
	public TrueResult queryEmployeesByName(final @RequestParam String name) {
		try {
			return commonTrueResult(securityService.queryEmployeesByName(name));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}
	
	/* 根据员工的name来筛选 模糊匹配 */
	@RequestMapping(value = "/employee/by-organization")
	@ResponseBody
	public TrueResult queryEmployeesByOrganization(final @RequestParam int organizationId) {
		try {
			return commonTrueResult(securityService.queryEmployeesByOrganization(organizationId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 根据员工的name来筛选 模糊匹配 分页 */
	@RequestMapping(value = "/employee/by-name/page")
	@ResponseBody
	public TrueResult pageQueryEmployeesByName(final @RequestParam String name, final @RequestParam int pageNum, final @RequestParam int pageSize) {
		try {
			return commonTrueResult(securityService.queryEmployeesByName(name, pageNum, pageSize));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/employee/add-roles")
	@ResponseBody
	public TrueResult addRolesToEmployee(final @RequestParam int employeeId, final @RequestParam String roleIds) {
		try {
			return booleanTrueResult(securityService.addRolesToEmployee(employeeId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(roleIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/employee/remove-roles")
	@ResponseBody
	public TrueResult removeRolesToEmployee(final @RequestParam int employeeId, final @RequestParam String roleIds) {
		try {
			return booleanTrueResult(securityService.removeRolesFromEmployee(employeeId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(roleIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/add-tree")
	@ResponseBody
	public TrueResult addRoleTree(@RequestParam Role role) {
		try {
			return integerTrueResult(securityService.addOrUpdateRole(role));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/add/by-json")
	@ResponseBody
	public TrueResult addRoleByJson(@RequestParam Role role) {
		role.setChildren(null);
		return addRole(role);
	}

	@RequestMapping(value = "/role/add")
	@ResponseBody
	public TrueResult addRole(@ModelAttribute Role role) {
		try {
			return integerTrueResult(securityService.addOrUpdateRole(role));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/remove")
	@ResponseBody
	public TrueResult removeRole(@RequestParam int id) {
		try {
			return booleanTrueResult(securityService.removeRole(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取员工 id不能为空 */
	@RequestMapping(value = "/role/one")
	@ResponseBody
	public TrueResult queryRole(final @RequestParam Integer id) {
		try {
			Validate.notNull(id);
			return commonTrueResult(securityService.queryRoleById(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取全部角色 */
	@RequestMapping(value = "/role/all-roots")
	@ResponseBody
	public TrueResult queryAllRootRoles() {
		try {
			return commonTrueResult(securityService.queryAllRootRoles());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	/* 获取全部角色 树结构 */
	@RequestMapping(value = "/role/all-roots-tree")
	@ResponseBody
	public TrueResult queryAllRootRolesWithChildren() {
		try {
			return commonTrueResult(securityService.queryAllRootRolesWithChildren());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/children")
	@ResponseBody
	public TrueResult queryChildrenForRole(@RequestParam int roleId) {
		try {
			return commonTrueResult(securityService.queryChildrenForRole(roleId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/by-organization")
	@ResponseBody
	public TrueResult queryRolesByOrganization(@RequestParam int organizationId) {
		try {
			return commonTrueResult(securityService.queryRolesByOrganization(organizationId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/add-employees")
	@ResponseBody
	public TrueResult addEmployeesToRole(final @RequestParam int roleId, final @RequestParam String employeeIds) {
		try {
			return booleanTrueResult(securityService.addEmployeesToRole(roleId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(employeeIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role/remove-employees")
	@ResponseBody
	public TrueResult removeEmployeesFromRole(final @RequestParam int roleId, final @RequestParam String employeeIds) {
		try {
			return booleanTrueResult(securityService.removeEmployeesFromRole(roleId, ArrayUtils.toObject(RequestUtil.convertStrToIntArray(employeeIds))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/add-tree")
	@ResponseBody
	public TrueResult addMenuItemTree(@RequestParam MenuItem menuItem) {
		try {
			return integerTrueResult(securityService.addOrUpdateMenuItem(menuItem));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/add/by-json")
	@ResponseBody
	public TrueResult addMenuItemByJson(@RequestParam MenuItem menuItem) {
		menuItem.setChildren(null);
		return addMenuItem(menuItem);
	}

	@RequestMapping(value = "/menuItem/add")
	@ResponseBody
	public TrueResult addMenuItem(@ModelAttribute MenuItem menuItem) {
		try {
			return integerTrueResult(securityService.addOrUpdateMenuItem(menuItem));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/remove")
	@ResponseBody
	public TrueResult removeMenuItem(@RequestParam int id) {
		try {
			return booleanTrueResult(securityService.removeMenuItem(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/batch-remove")
	@ResponseBody
	public TrueResult removeMenuItem(@RequestParam String ids) {
		try {
			return booleanTrueResult(securityService.batchRemoveMenuItem(ArrayUtils.toObject(RequestUtil.convertStrToIntArray(ids))));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/one")
	@ResponseBody
	public TrueResult queryMenuItem(final @RequestParam Integer id) {
		try {
			Validate.notNull(id);
			return commonTrueResult(securityService.queryMenuItemById(id));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/all-roots")
	@ResponseBody
	public TrueResult queryAllRootMenuItems() {
		try {
			return commonTrueResult(securityService.queryAllRootMenuItems());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/all-roots-tree")
	@ResponseBody
	public TrueResult queryAllRootMenuItemsWithChildren() {
		try {
			return commonTrueResult(securityService.queryAllRootMenuItemsWithChildren());
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/menuItem/children")
	@ResponseBody
	public TrueResult queryChildrenForMenuItem(@RequestParam int menuItemId) {
		try {
			return commonTrueResult(securityService.queryChildrenForMenuItem(menuItemId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role-menuItem/add")
	@ResponseBody
	public TrueResult addOrUpdateRoleMenuItem(@RequestParam int roleId, @RequestParam int menuItemId, @RequestParam String power_expand) {
		try {
			return integerTrueResult(securityService.addOrUpdateRoleMenuItem(roleId, menuItemId, power_expand));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/role-menuItem/remove")
	@ResponseBody
	public TrueResult removeRoleMenuItem(@RequestParam int roleMenuItemId) {
		try {
			return booleanTrueResult(securityService.removeRoleMenuItem(roleMenuItemId));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/login")
	@ResponseBody
	public TrueResult login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
		try {
			Employee e = securityService.queryEmployeeByUsername(username);
			if (e != null) {
				boolean isAuth = securityService.auth(username, password);
				if (isAuth) {
					request.getSession().setAttribute("currentEmployee", e);
					return new TrueResult(currentRequestUrl(), OK_PREIX, "用户登录成功");
				} else {
					return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户登录失败");
				}
			} else {
				return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户不存在");
			}
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/loginout")
	@ResponseBody
	public TrueResult loginout(@RequestParam String username, HttpServletRequest request) {
		try {
			request.getSession().removeAttribute("currentEmployee");
			return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户注销成功");
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/change-password")
	@ResponseBody
	public TrueResult changePassword(@RequestParam String username, @RequestParam String oldPassword, @RequestParam String newPassword) {
		try {
			Employee e = securityService.queryEmployeeByUsername(username);
			if (e != null) {
				boolean isAuth = securityService.auth(username, oldPassword);
				if (isAuth) {
					return booleanTrueResult(securityService.changePassword(username, oldPassword, newPassword));
				} else {
					return new TrueResult(currentRequestUrl(), FAIL_PREIX, "旧密码不正确");
				}
			} else {
				return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户不存在");
			}
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/change-lockstate")
	@ResponseBody
	public TrueResult changeLockState(@RequestParam String username, @RequestParam boolean isLock) {
		try {
			return booleanTrueResult(securityService.changeLockState(username, isLock));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/lockstate")
	@ResponseBody
	public TrueResult getLockState(@RequestParam String username) {
		try {
			return commonTrueResult(securityService.getLockState(username));
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/permission-tree")
	@ResponseBody
	public TrueResult getPermissionTree(@RequestParam String username) {
		try {
			Employee e = securityService.queryEmployeeByUsername(username);
			if (e != null) {
				return commonTrueResult(securityService.getPermissionTree(username));
			} else {
				return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户不存在");
			}
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}

	@RequestMapping(value = "/user/login-return-permission")
	@ResponseBody
	public TrueResult loginAndReturnPermission(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
		try {
			Employee e = securityService.queryEmployeeByUsername(username);
			if (e != null) {
				boolean isAuth = securityService.auth(username, password);
				if (isAuth) {
					request.getSession().setAttribute("currentEmployee", e);
					return commonTrueResult(securityService.getPermissionTree(username));
				} else {
					return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户登录失败");
				}
			} else {
				return new TrueResult(currentRequestUrl(), FAIL_PREIX, "用户不存在");
			}
		} catch (Throwable e) {
			LOGGER.debug(e);
			return errorTrueResult(e.getLocalizedMessage());
		}
	}
	
	@JsonAutoDetect(fieldVisibility = Visibility.ANY)
	@SuppressWarnings("unused")
	private static class TrueResult {

		private String call;
		private String state;
		private Object result;

		public TrueResult(@JsonProperty String call, @JsonProperty String state, @JsonProperty Object result) {
			super();
			this.call = call;
			this.state = state;
			this.result = result;
		}

		public String getCall() {
			return call;
		}

		public void setCall(String call) {
			this.call = call;
		}

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public Object getResult() {
			return result;
		}

		public void setResult(Object result) {
			this.result = result;
		}
	}
}
